#include "subrw1.h"

SubRW1::SubRW1(QWidget *parent)
    :RenderWidget(parent)
{

}


void SubRW1::UploadMesh()
{
    QOpenMesh* theMesh = static_cast<QOpenMesh*>(m_mm->Meshes()[NOISED]);
    OMesh* theOpenMesh = theMesh->GetMesh();

    VertexIter vi = theOpenMesh->vertices_begin();

    OpenMesh::Vec3f bbMax, bbMin;
    bbMax = bbMin = OpenMesh::vector_cast<OpenMesh::Vec3f>(theOpenMesh->point(vi));
    for (;vi != theOpenMesh->vertices_end(); ++vi)
    {
     bbMin.minimize(OpenMesh::vector_cast<OpenMesh::Vec3f>(theOpenMesh->point(vi)));
     bbMax.maximize(OpenMesh::vector_cast<OpenMesh::Vec3f>(theOpenMesh->point(vi)));
    }
    const float* center = ((bbMax+bbMin)*0.5).data();
    float length = (bbMin-bbMax).norm();

    m_eye = QVector3D(0,0,length);
    m_center = QVector3D(center[0],center[1],center[2]);
    m_up = QVector3D(0,1.f,0);

    m_view.setToIdentity();
    m_view.lookAt(m_eye,m_center,m_up);

    theMesh->UploadArrayData(QVERTEX);
    theMesh->UploadArrayData(QNORMAL);
    theMesh->UploadElementData(QINDICES);
}
